/***************************************************************
 * Name:      DialogueHighLightMain.cpp
 * Purpose:   Code for Application Frame
 * Author:    jacky (248023406@qq.com)
 * Created:   2011-07-11
 * Copyright: jacky (http://weibo.com/sleepiforest)
 * License:
 **************************************************************/

#include "wx_pch.h"
#include "DialogueHighLightMain.h"
#include <wx/msgdlg.h>
#include <cstdio>
#include <wx/textfile.h>
#include <wx/fontdlg.h>
//(*InternalHeaders(DialogueHighLightFrame)
#include <wx/intl.h>
#include <wx/string.h>
//*)

//helper functions
enum wxbuildinfoformat
{
    short_f, long_f
};

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

//(*IdInit(DialogueHighLightFrame)
const long DialogueHighLightFrame::ID_STATICTEXT1 = wxNewId();
const long DialogueHighLightFrame::ID_TEXTCTRL1 = wxNewId();
const long DialogueHighLightFrame::ID_BUTTON1 = wxNewId();
const long DialogueHighLightFrame::ID_STATICTEXT2 = wxNewId();
const long DialogueHighLightFrame::ID_TEXTCTRL2 = wxNewId();
const long DialogueHighLightFrame::ID_BUTTON2 = wxNewId();
const long DialogueHighLightFrame::ID_PANEL1 = wxNewId();
const long DialogueHighLightFrame::ID_BUTTON5 = wxNewId();
const long DialogueHighLightFrame::ID_BUTTON3 = wxNewId();
const long DialogueHighLightFrame::ID_PANEL3 = wxNewId();
const long DialogueHighLightFrame::ID_BUTTON4 = wxNewId();
const long DialogueHighLightFrame::ID_SCROLLEDWINDOW1 = wxNewId();
const long DialogueHighLightFrame::idMenuQuit = wxNewId();
const long DialogueHighLightFrame::idMenuSetting = wxNewId();
const long DialogueHighLightFrame::idMenuAbout = wxNewId();
const long DialogueHighLightFrame::ID_STATUSBAR1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(DialogueHighLightFrame,wxFrame)
    //(*EventTable(DialogueHighLightFrame)
    //*)
END_EVENT_TABLE()

DialogueHighLightFrame::DialogueHighLightFrame(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(DialogueHighLightFrame)
    wxBoxSizer* BoxSizer4;
    wxBoxSizer* BoxSizer5;
    wxMenuItem* MenuItem2;
    wxMenuItem* MenuItem1;
    wxBoxSizer* BoxSizer2;
    wxMenu* Menu1;
    wxBoxSizer* BoxSizer1;
    wxBoxSizer* BoxSizer3;
    wxMenu* Menu2;

    Create(parent, wxID_ANY, _("对白高亮程序~超级简陋のv0.8无责任测试版"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE|wxMINIMIZE_BOX, _T("wxID_ANY"));
    SetClientSize(wxSize(640,227));
    SetIcon(wxIcon(_("aaaa")));
    BoxSizer1 = new wxBoxSizer(wxVERTICAL);
    Panel1 = new wxPanel(this, ID_PANEL1, wxDefaultPosition, wxDefaultSize, wxDOUBLE_BORDER|wxTAB_TRAVERSAL, _T("ID_PANEL1"));
    BoxSizer2 = new wxBoxSizer(wxVERTICAL);
    BoxSizer3 = new wxBoxSizer(wxHORIZONTAL);
    StaticText1 = new wxStaticText(Panel1, ID_STATICTEXT1, _("源对白文件："), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE, _T("ID_STATICTEXT1"));
    StaticText1->SetMinSize(wxSize(90,-1));
    BoxSizer3->Add(StaticText1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    txInputFile = new wxTextCtrl(Panel1, ID_TEXTCTRL1, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
    BoxSizer3->Add(txInputFile, 4, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    Button1 = new wxButton(Panel1, ID_BUTTON1, _("打开"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
    BoxSizer3->Add(Button1, 1, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer2->Add(BoxSizer3, 1, wxALL|wxEXPAND|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
    BoxSizer4 = new wxBoxSizer(wxHORIZONTAL);
    StaticText2 = new wxStaticText(Panel1, ID_STATICTEXT2, _("目标输出文件："), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE, _T("ID_STATICTEXT2"));
    StaticText2->SetMinSize(wxSize(90,-1));
    BoxSizer4->Add(StaticText2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    txOutputFile = new wxTextCtrl(Panel1, ID_TEXTCTRL2, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL2"));
    BoxSizer4->Add(txOutputFile, 4, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
    Button2 = new wxButton(Panel1, ID_BUTTON2, _("打开"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON2"));
    BoxSizer4->Add(Button2, 1, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer2->Add(BoxSizer4, 1, wxALL|wxEXPAND|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
    Panel1->SetSizer(BoxSizer2);
    BoxSizer2->Fit(Panel1);
    BoxSizer2->SetSizeHints(Panel1);
    BoxSizer1->Add(Panel1, 0, wxALL|wxEXPAND|wxALIGN_LEFT|wxALIGN_BOTTOM, 0);
    Panel3 = new wxPanel(this, ID_PANEL3, wxDefaultPosition, wxSize(615,40), wxDOUBLE_BORDER|wxTAB_TRAVERSAL, _T("ID_PANEL3"));
    Panel3->SetMinSize(wxSize(615,40));
    BoxSizer5 = new wxBoxSizer(wxHORIZONTAL);
    Button5 = new wxButton(Panel3, ID_BUTTON5, _("自动识别"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON5"));
    Button5->SetForegroundColour(wxColour(255,0,0));
    BoxSizer5->Add(Button5, 1, wxALL|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
    Button3 = new wxButton(Panel3, ID_BUTTON3, _("生成"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON3"));
    BoxSizer5->Add(Button3, 1, wxALL|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
    Panel3->SetSizer(BoxSizer5);
    BoxSizer5->SetSizeHints(Panel3);
    BoxSizer1->Add(Panel3, 0, wxALL|wxEXPAND|wxFIXED_MINSIZE|wxALIGN_LEFT|wxALIGN_BOTTOM, 0);
    Button4 = new wxButton(this, ID_BUTTON4, _("新建角色"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON4"));
    Button4->SetMinSize(wxSize(605,30));
    BoxSizer1->Add(Button4, 0, wxALL|wxEXPAND|wxFIXED_MINSIZE|wxALIGN_LEFT|wxALIGN_BOTTOM, 1);
    RoleContainer = new wxScrolledWindow(this, ID_SCROLLEDWINDOW1, wxDefaultPosition, wxDefaultSize, wxVSCROLL|wxSUNKEN_BORDER|wxNO_FULL_REPAINT_ON_RESIZE|wxFULL_REPAINT_ON_RESIZE, _T("ID_SCROLLEDWINDOW1"));
    GridSizer1 = new wxGridSizer(0, 2, 0, 0);
    RoleContainer->SetSizer(GridSizer1);
    GridSizer1->Fit(RoleContainer);
    GridSizer1->SetSizeHints(RoleContainer);
    BoxSizer1->Add(RoleContainer, 0, wxALL|wxEXPAND|wxALIGN_LEFT|wxALIGN_BOTTOM, 0);
    SetSizer(BoxSizer1);
    MenuBar1 = new wxMenuBar();
    Menu1 = new wxMenu();
    MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("退出\tAlt-F4"), _("退出程序~"), wxITEM_NORMAL);
    Menu1->Append(MenuItem1);
    MenuBar1->Append(Menu1, _("&文件"));
    Menu3 = new wxMenu();
    MenuItem3 = new wxMenuItem(Menu3, idMenuSetting, _("字体设置"), _("设置字体属性"), wxITEM_NORMAL);
    Menu3->Append(MenuItem3);
    MenuBar1->Append(Menu3, _("选项"));
    Menu2 = new wxMenu();
    MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("关于\tF1"), _("程序作者相关信息"), wxITEM_NORMAL);
    Menu2->Append(MenuItem2);
    MenuBar1->Append(Menu2, _("帮助"));
    SetMenuBar(MenuBar1);
    StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
    int __wxStatusBarWidths_1[1] = { -1 };
    int __wxStatusBarStyles_1[1] = { wxSB_NORMAL };
    StatusBar1->SetFieldsCount(1,__wxStatusBarWidths_1);
    StatusBar1->SetStatusStyles(1,__wxStatusBarStyles_1);
    SetStatusBar(StatusBar1);
    sourceFileChooser = new wxFileDialog(this, _("Select file"), wxEmptyString, wxEmptyString, _("Text Files(*.txt)|*txt"), wxFD_OPEN|wxFD_FILE_MUST_EXIST, wxDefaultPosition, wxDefaultSize, _T("wxFileDialog"));
    targetFileChooser = new wxFileDialog(this, _("Select file"), wxEmptyString, wxEmptyString, _("HTML files(*.html)|*html"), wxFD_SAVE|wxFD_OVERWRITE_PROMPT, wxDefaultPosition, wxDefaultSize, _T("wxFileDialog"));
    BoxSizer1->SetSizeHints(this);

    Connect(ID_TEXTCTRL1,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&DialogueHighLightFrame::OnTextCtrl1Text);
    Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&DialogueHighLightFrame::OnButton1Click);
    Connect(ID_BUTTON2,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&DialogueHighLightFrame::OnButton2Click);
    Connect(ID_BUTTON5,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&DialogueHighLightFrame::OnButton5Click);
    Connect(ID_BUTTON3,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&DialogueHighLightFrame::OnButton3Click);
    Connect(ID_BUTTON4,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&DialogueHighLightFrame::OnButton4Click);
    Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&DialogueHighLightFrame::OnQuit);
    Connect(idMenuSetting,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&DialogueHighLightFrame::OnFontSetting);
    Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&DialogueHighLightFrame::OnAbout);
    //*)
//    Connect(wxEVT_SIZE,(wxObjectEventFunction)&DialogueHighLightFrame::OnResize);
    roleCnt = 0;
    AddRolePanel(new KeyHLPanel(RoleContainer));
    AddRolePanel(new KeyHLPanel(RoleContainer));
    mainFont.SetFaceName(_("微软雅黑"));
}


DialogueHighLightFrame::~DialogueHighLightFrame()
{
    //(*Destroy(DialogueHighLightFrame)
    //*)
}
void DialogueHighLightFrame::OnQuit(wxCommandEvent& event)
{
    Close();
}

void DialogueHighLightFrame::OnAbout(wxCommandEvent& event)
{
    wxString msg = _("软件作者：沉睡森林@SCAU_09CSNT5\n\n欢迎联系qq:248023406\n\n提出各种意见~~");
    wxMessageBox(msg, _("关于"));
}

void DialogueHighLightFrame::OnTextCtrl2Text(wxCommandEvent& event)
{
}

void DialogueHighLightFrame::OnTextCtrl1Text(wxCommandEvent& event)
{
}

void DialogueHighLightFrame::OnPanel3Paint(wxPaintEvent& event)
{
}

void DialogueHighLightFrame::OnPanel3Paint1(wxPaintEvent& event)
{
}

void DialogueHighLightFrame::OnButton1Click(wxCommandEvent& event)
{
    if(sourceFileChooser->ShowModal()==wxID_OK)
    {
        txInputFile->SetValue(sourceFileChooser->GetPath());
        const wxString& path = sourceFileChooser->GetDirectory();
        wxString name = sourceFileChooser->GetFilename();
        name = name.SubString(0,name.Len()-5)+_(".html");
        targetFileChooser->SetFilename(name);
        txOutputFile->SetValue(path+_("\\")+name);
    }
}

void DialogueHighLightFrame::OnButton2Click(wxCommandEvent& event)
{
    if(targetFileChooser->ShowModal()==wxID_OK)
    {
        wxString path = targetFileChooser->GetPath();
        if(path.Find(_("."))!=wxNOT_FOUND) path+=_(".html");
        txOutputFile->SetValue(targetFileChooser->GetPath());
    }
}

void DialogueHighLightFrame::OnButton4Click(wxCommandEvent& event)
{
    if(roleCnt>=MaxRole)
    {
        wxMessageBox(_("此版本最多只能添加26个角色~\n\n欢迎联系QQ：248023406 沉睡森林"));
        return;
    }
    AddRolePanel(new KeyHLPanel(RoleContainer));
}
void DialogueHighLightFrame::AddRolePanel(KeyHLPanel* pl)
{
   // RoleContainer->Show(false);
    BnIdToWnd[pl->GetId()]=pl;
   // printf("add id=%d pl=%u\n",pl->BnClose->GetId(),pl);
    Connect(pl->GetId(),wxEVT_COMMAND_BUTTON_CLICKED,
            (wxObjectEventFunction)&DialogueHighLightFrame::DeleteRolePanel);
    wxSizer* sizer = RoleContainer->GetSizer();
    sizer->Add(pl, 0, wxALL|wxEXPAND|wxALIGN_LEFT|wxALIGN_BOTTOM, 2);
    //pl->Show();
    //RoleContainer->Show();
    FitSize();
    roleCnt++;
}
void DialogueHighLightFrame::FitSize()
{
    wxSizer* sizer = RoleContainer->GetSizer();
    RoleContainer->SetMinSize(sizer->GetMinSize());
    if(!this->IsMaximized())
    {
        this->GetSizer()->SetSizeHints(this);
    }
    else
    {
        this->SetMinSize(this->DoGetBestSize());
        this->GetSizer()->Layout();
    }
    sizer->Layout();
}
void DialogueHighLightFrame::DeleteRole(int id)
{
    if(!BnIdToWnd[id]->Destroy()) wxMessageBox(_("destroy error"));
    Disconnect(id,wxEVT_COMMAND_BUTTON_CLICKED,
            (wxObjectEventFunction)&DialogueHighLightFrame::DeleteRolePanel);
    FitSize();
    roleCnt--;
}
void DialogueHighLightFrame::DeleteRolePanel(wxCommandEvent& event)
{
    int id = event.GetId();
    DeleteRole(id);
    BnIdToWnd.erase(id);
}
void DialogueHighLightFrame::Clear()
{
    map<int,KeyHLPanel*>::iterator it;
    for(it=BnIdToWnd.begin();it!=BnIdToWnd.end();++it)
    {
        int id = it->first;
        DeleteRole(id);
    }
    BnIdToWnd.clear();
}
void DialogueHighLightFrame::SetKeys()
{
    analyser.Clear();
    map<int,KeyHLPanel*>::iterator it;
    for(it=BnIdToWnd.begin();it!=BnIdToWnd.end();++it)
    {
        KeyHLPanel *pl = it->second;
        analyser.Add(pl->GetRoleName(),
                     KeyInfo(pl->GetTextColour(),
                             pl->GetTextBgColour()));
    }
}
void DialogueHighLightFrame::OnButton3Click(wxCommandEvent& event)
{
    wxTextFile inputFile(txInputFile->GetValue());
    if(!inputFile.Open(wxConvLocal)&&!inputFile.Open())
    {
       // wxMessageBox(_("输入文件不存在！！"));
        return;
    }
    wxTextFile outputFile(txOutputFile->GetValue());
    if(!outputFile.Create()&&!outputFile.Open())
    {
        wxMessageBox(_("无法创建输出文件！！"));
        return;
    }
    SetKeys();
    analyser.TransForm(inputFile,outputFile);
    inputFile.Close();
    outputFile.Close();
    wxMessageBox(_("o(∩_∩)o 生成成功！！"));
}
void DialogueHighLightFrame::ReadRole()
{
    this->Freeze();
    Clear();
    map<wxString,KeyInfo>::iterator it;
    for(it=analyser.keys.begin();it!=analyser.keys.end();++it)
    {
        if(roleCnt>=MaxRole)
        {
            wxMessageBox(_("此版本最多只能添加26个角色~\n\n欢迎联系QQ：248023406 沉睡森林"));
            break;
        }
        AddRolePanel(new KeyHLPanel(RoleContainer,it->first,
                                    it->second.fg));
    }
    this->Thaw();
    this->Refresh();
}
void DialogueHighLightFrame::OnButton5Click(wxCommandEvent& event)
{
    wxTextFile inputFile(txInputFile->GetValue());
    //this->MakeModal(true);
    if(!inputFile.Open(wxConvLocal)&&!inputFile.Open())
    {
       // wxMessageBox(_("输入文件不存在！！"));
        return;
    }
    //RoleContainer->Show(false);
    analyser.AutoAnalyse(inputFile);
   // RoleContainer->Show();
    inputFile.Close();
    ReadRole();
}

void DialogueHighLightFrame::OnFontSetting(wxCommandEvent& event)
{
    wxFontData fd;
    fd.SetInitialFont(mainFont);
    wxFontDialog fdlg(this,fd);
    if(fdlg.ShowModal()==wxID_OK)
    {
        mainFont = fdlg.GetFontData().GetChosenFont();
    }
}
